// JavaScript Document
// document.write('<script type="text/javascript" src="../static/js/check_input.js"></script>')
$(function(){

	/*密码明文和秘文显示*/
	$('.password-eye').on('click', function(){
		if($('.form-control.pwd').attr('type') == 'password'){
			$('.form-control.pwd').attr('type', 'text');
			$('.password-eye').attr('class','glyphicon glyphicon-eye-open form-control-feedback password-eye');
		}else{
			$('.form-control.pwd').attr('type', 'password');
			$('.password-eye').attr('class','glyphicon glyphicon-eye-close form-control-feedback password-eye');
		}
	});


    /*初始化验证码*/
    getCaptcha();
    /*换一张验证码*/
    $('#codeImg').on('click', function() {
        getCaptcha();
    })
	/*获取captcha验证码*/
    function getCaptcha() {
        $.ajax({
            url: '/captcha/getCaptchaCode',
            method: 'get',
            contentType:'application/json',
            cache: false,
            success: function(data) {
                $('#codeImg').attr('src', 'data:image/jpeg;base64,' + data);
            },
            error: function(err) {
                alert('接口回调失败');
            }
        })
    }
	/*监听input输入框开启登录按钮*/
	$('#username').on('input propertychange change', function(){
		var uname = $(this).val();
		var pwd = $('#password').val();
		if(uname !== '' && pwd !== ''){
			$('#btnLogin').removeAttr('disabled');
		}else{
			$('#btnLogin').attr('disabled', true);
		}
	});
	$('#password').on('input propertychange change', function(){
		var uname = $('#username').val();
		var pwd = $(this).val();
		if(uname !== '' && pwd !== ''){
			$('#btnLogin').removeAttr('disabled');
		}else{
			$('#btnLogin').attr('disabled', true);
		}
	});
	
	/*登录按钮：请求登录账号*/
	$('#btnLogin').on('click', function(){
		var uname = $('#username').val();
		var pwd = $('#password').val();
		var code = $('#code').val();
        var cToken = $.cookie('CTOKEN');
		if(code === '' || code === null){
			$('#error_code').html('<i class="fa fa-times-circle"></i>请输入验证码');
			return;
		}
        if((uname == '' || uname == null) || (pwd == '' || pwd == null)){
            $('#error_code').html('<i class="fa fa-times-circle"></i>用户名和密码均不能为空');
            getCaptcha();
            return;
        }
        if(cToken == '' || cToken == null){
            $('#error_code').html('<i class="fa fa-times-circle"></i>验证码已过期');
            getCaptcha();
            return;
        }
		$.ajax({
			url:'/loginnew/login',
			method:'post',
            contentType:'application/json;charset=utf-8',
			data:JSON.stringify({
                'username' : uname,
                'password' : pwd,
                'code' : code,
                'cToken' : cToken
			}),
			dataType:'json',
			success: function(data){
                var code = data.code;
                var data = data.data;
                if(code === 1002){
                    var error_up = data.error_up;
                    var error_code = data.error_code;
                	if(error_up!=undefined) $('#error_up').html('<i class="fa fa-times-circle"></i>' + error_up);
                    if(error_code!=undefined) $('#error_code').html('<i class="fa fa-times-circle"></i>' + error_code);
				} else if(code === 1001){
                	location.href = '/user/index';
				}
			},
			error: function(err){
				alert("回调函数失败");
			}
		})
	});

})

/**
 * 获取焦点清空错误提示
 */
function removeHint() {
	$('#error_code, #error_up').html('');

}